package com.lnsf.bookstore.system.comment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.system.comment.dao.CommentMapper;
import com.lnsf.bookstore.system.comment.service.CommentService;
import com.lnsf.bookstore.system.comment.vo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Comment queryObject(Integer id) {
        return commentMapper.queryObject(id);
    }

    @Override
    public int save(Comment comment) {
        return commentMapper.insertSelective(comment);
    }

    @Override
    public int update(Comment comment) {
        return commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Override
    public int delete(Integer id) {
        return commentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return commentMapper.deleteBatch(ids);
    }

    @Override
    public int toggleStatus(Comment comment) {
        Comment commentEntity = queryObject(comment.getId());
        if (commentEntity.getStatus() == 1) {
            commentEntity.setStatus(0);
        } else if (commentEntity.getStatus() == 0) {
            commentEntity.setStatus(1);
        }
        return commentMapper.updateByPrimaryKey(commentEntity);
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {
        return commentMapper.queryTotal(map);
    }

    @Override
    public PageInfo<Comment> getPage(Map<String, Object> map) {
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
        return new PageInfo<Comment>(commentMapper.queryList(map));
    }
}
